Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Batch email notifications #276

Open
wants to merge 15 commits into
base: gsoc24
Choose a base branch
from
Open

[feat] Batch email notifications #276

wants to merge 15 commits into from

Conversation

Dhanus3133
Copy link
Contributor

Fixes #132

Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dhanus3133 I have left some suggestions. I hope these solve your queries.

openwisp_notifications/settings.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
@Dhanus3133 Dhanus3133 marked this pull request as ready for review June 6, 2024 14:56
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dhanus3133 please start writin a short README section that explains how this feature works and add the new setting, remember to add a screenshot (push it to the docs branch).

@pandafy you know what would be handy?

  • Allow to change the batching using an organization setting that allows user to change this per org via the admin
  • Allow to specify whether the system should start batching right away instead of waiting 1 email, this could be disabled by default, and could also be available from the organization page

openwisp_notifications/handlers.py Outdated Show resolved Hide resolved

# Check if the rest of the notifications are sent in a batch
self.assertEqual(len(mail.outbox), 2)
self.assertEqual(mail.outbox[1].subject, "Summary of Notifications")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd try to explore the possibility of using freezegun to simulate the passing of time and ensure that a new notification sent after the batch interval is not batched.

@Dhanus3133
Copy link
Contributor Author

In our weekly call, @nemesifier and @pandafy suggested some improvements.

  1. The date should be displayed below the title.
  2. Update the email title with the number of notifications and site name.
  3. Some changes have to be made regarding the email template
    1. If there is only one notification in the batch, just use a single mail template.
    2. Can follow the current template if there are 2 - 5 notifications.
    3. If there are more than 5 emails, a new template can be used where only the level, message, and date can be displayed. Along with a ‘View all new notifications’ link to the admin site.

Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good. I have done a quick code review and left some suggestion on improving the code.

I will do a functional test tomorrow and report if I find something to improve.

openwisp_notifications/settings.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Show resolved Hide resolved
@@ -37,6 +37,10 @@
'openwisp-notifications/audio/notification_bell.mp3',
)

OPENWISP_NOTIFICATIONS_EMAIL_BATCH_INTERVAL = getattr(
settings, 'EMAIL_BATCH_INTERVAL', 30 * 60 # 30 minutes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking out loud

I think 30 minutes is a lot of time. It will affect the real-timeness of the email notifications. Most of the consecutive notifications would be delayed by this time. We should also point this out in the README.

@nemesifier what do you think?

Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediate email after batch email

If a notification is created just after the batch email is processed, then the email for this notification is sent immediately after the batch email.

Is this a desired behaviour? Can you please add a test for this?

Duplicate subject in email

The send_email function adds a subject in the email which servers as a heading for all the email communication.

Screenshot from 2024-06-18 13-05-59

I think, we should keep using the subject provided by the send_email function for consistency.

openwisp_notifications/settings.py Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
openwisp_notifications/templates/emails/batch_email.html Outdated Show resolved Hide resolved
openwisp_notifications/templates/emails/batch_email.html Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/utils.py Outdated Show resolved Hide resolved
openwisp_notifications/tests/test_notifications.py Outdated Show resolved Hide resolved
Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dhanus3133 the test is failing because the notification email contains direct URL to the target object while the test expects redirect url

-<div class="msg"><p>Default notification with default verb and level info by <a href="https://example.com/api/v1/notifications/notification/2632e308-0f2d-43a1-8617-7350e48b8fd3/redirect/">Tester Tester (test org)</a></p></div>
+<div class="msg"><p>Default notification with default verb and level info by <a href="https://example.com/admin/openwisp_users/organizationuser/dea5c3cc-8c2a-4021-94a7-9cc36885f9ff/change/">Tester Tester (test org)</a></p></div>

@Dhanus3133 Dhanus3133 changed the base branch from master to gsoc24 June 23, 2024 13:30
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @Dhanus3133 @pandafy, see my comments below.

README.rst Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/settings.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/templates/emails/batch_email.txt Outdated Show resolved Hide resolved
openwisp_notifications/utils.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
self.assertIn('Summary of 10 Notifications', mail.outbox[1].subject)
self.assertIn('View all Notifications', mail.outbox[1].body)
self.assertNotIn('Test Notification', mail.outbox[1].body)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add another test in which this feature is disabled and ensure that no batching happens in that case?
Using the override_settings from django or mocking the setting should work.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I had asked before, but I am not sure I put in the review, I think we should add a way to open the web UI of openwisp and trigger the opening of the notification widget automatically, so that we can use that in the email: after clicking on the button to see all the remaining notifications, the user is taken to the application and the notification widget opens automatically to show the notifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] Batch email notifications to prevent email flooding
3 participants